package com.yxhl.zoume.core.tripsmgmt.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;

import com.yxhl.protobuf.OrderBizType;
import com.yxhl.zoume.R;
import com.yxhl.zoume.common.ui.activity.base.BaseActivityWithToolBar;
import com.yxhl.zoume.core.func.webpage.info.WebPageEntrance;
import com.yxhl.zoume.core.tripsmgmt.ui.event.ComeBackFromTicketCodeEvent;
import com.yxhl.zoume.core.tripsmgmt.ui.fragment.home.TicketQRCodeFragment;
import com.yxhl.zoume.data.rxbus.RxBus;
import com.yxhl.zoume.di.component.base.HasComponent;
import com.yxhl.zoume.di.component.trip.DaggerTicketQRComponent;
import com.yxhl.zoume.di.component.trip.TicketQRComponent;
import com.yxhl.zoume.di.module.trip.TicketQRModule;

import butterknife.BindView;

/**
 * created by sugarya. Email:sugarya@foxmail.com
 * 我的车票二维码 条形码展示
 */
public class TicketCodeActivity extends BaseActivityWithToolBar implements HasComponent<TicketQRComponent> {

    public static final String KEY_ORDER_ID = "intent_key_verify_code";
    public static final String KEY_ORDER_BIZ_TYPE = "intent_key_verify_code_biz_type";

    @BindView(R.id.frame_activity_ticket_code)
    FrameLayout mFrameLayout;

    //arguments
    private TicketQRComponent component;
    private OrderBizType bizType;
    private String orderId;

    public static Intent getCallingIntent(Context context, String tripOrder, OrderBizType bizType) {
        Intent launch = new Intent(context, TicketCodeActivity.class);
        Bundle bundle = new Bundle();
        bundle.putCharSequence(KEY_ORDER_ID, tripOrder);
        bundle.putSerializable(KEY_ORDER_BIZ_TYPE, bizType);
        launch.putExtras(bundle);
        return launch;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        handleIntent();
        addFragment(R.id.frame_activity_ticket_code, TicketQRCodeFragment.newInstance(orderId, bizType));
    }

    private void handleIntent() {
        Intent intent = getIntent();
        if (intent != null) {
            Bundle bundle = intent.getExtras();
            orderId = bundle.getString(KEY_ORDER_ID);
            bizType = (OrderBizType) bundle.getSerializable(KEY_ORDER_BIZ_TYPE);
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        overridePendingTransition(R.anim.left_enter, R.anim.right_out);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_ticket_code;
    }


    @Override
    protected void initializeInjector() {
        component = DaggerTicketQRComponent.builder()
                .appComponent(getAppComponent())
                .activityModule(getActivityModule())
                .ticketQRModule(new TicketQRModule())
                .build();
    }

    @Override
    protected void onHeaderLeftClick(View view) {
        onBackPressed();
        RxBus.getInstance().send(new ComeBackFromTicketCodeEvent());
    }

    @Override
    protected void onHeaderRightClick(View view) {
        if (view != null) {
            getAppComponent().navigator().navigateToWebPageActivity(this, WebPageEntrance.TICKET_CODE_HELP);
        }
    }

    @Override
    public TicketQRComponent getComponent() {
        return component;
    }
}
